#!/bin/bash -e

[ -f ./env ] && source ./env

TARGET=netspeeder
list=`ip link | sed -n '/^[^ ]/p' | awk '{print $2}' | awk -F: '{print $1}'`; 
index=0; 
for i in ${list}; do
    index=$((index+1));
    echo ${index}.$i;
done;

default_in_intf=${in_intf}
in_intf=""
t="";
while [ -z "$in_intf" ]; do
    while :; do
        echo -n "choose one in_intf: [default ${default_in_intf}] "; read t;
        [ -n "${default_in_intf}" ] && break;
        [ -n "${t}" ] && break;
    done
    if [ ! "x" = "x${t}" ]; then
        index=0; 
        for i in ${list}; do
            index=$((index+1));
            if [ x${t} = x${index} ]; then in_intf=${i}; break;
            fi
        done
    else
        in_intf=${default_in_intf}
    fi
done
out_intf="-o $in_intf"
in_intf="-i $in_intf"

repeat_count=1;                    echo -n "input repeat_count: [default ${repeat_count}] "; read tmp; [ -z "${tmp}" ] || repeat_count=$tmp;
repeat_count="-r $repeat_count"

filter=${filter:="dst port 2003"}; echo -n "input filter: [default ${filter}] ";             read tmp; [ -z "${tmp}" ] || filter=$tmp; 
filter="-f '$filter'"

echo "[Unit]"                      >/dev/shm/netspeeder-tmp.service;
echo "After=network.target"       >>/dev/shm/netspeeder-tmp.service;
echo "StartLimitIntervalSec=0"    >>/dev/shm/netspeeder-tmp.service;
echo "[Service]"                  >>/dev/shm/netspeeder-tmp.service;
echo "LimitNOFILE=65535"          >>/dev/shm/netspeeder-tmp.service;
echo "Restart=always"             >>/dev/shm/netspeeder-tmp.service;
echo "RestartSec=3"               >>/dev/shm/netspeeder-tmp.service;
echo "ExecStart=/bin/${TARGET} $in_intf $out_intf $repeat_count $filter" >>/dev/shm/netspeeder-tmp.service ;
echo "[Install]"                  >>/dev/shm/netspeeder-tmp.service;
echo "WantedBy=multi-user.target" >>/dev/shm/netspeeder-tmp.service;
echo "====================== CONFIG FILE START========================";
cat /dev/shm/netspeeder-tmp.service;
echo "====================== CONFIG FILE  END ========================";
echo "already installed list: ";
cd /etc/systemd/system/multi-user.target.wants/; list=`ls netspeeder-* | cut -b 12- | awk '-F.' '{ print $1 }'`; cd - >/dev/null; 
index=0; 
for i in ${list}; do
    index=$((index+1));
    echo ${index}.$i;
done;
while :; do
    echo -n "choose one to overwrite or input new name: "; read name; [ -n "${name}" ] && break;
done;
index=0; 
for i in ${list}; do
    index=$((index+1));
    if [ x${name} = x${index} ]; then
        name=${i}; break;
    fi;
done;
while :; do
    echo -n "will create /lib/systemd/system/netspeeder-${name}.service and enable it, is that ok? [yes/no] "; read ok; [ "x${ok}" = xyes ] && break; [ "x${ok}" = xno ] && exit 1;
done;
rm /lib/systemd/system/netspeeder-${name}.service -rf; mv /dev/shm/netspeeder-tmp.service /lib/systemd/system/netspeeder-${name}.service;
rm /bin/${TARGET} -rf; cp -arpf ${TARGET} /bin/
echo systemctl daemon-reload;            systemctl daemon-reload;
rm /etc/systemd/system/multi-user.target/netspeeder-${name}.service -rf;
echo systemctl enable netspeeder-${name} ; systemctl enable netspeeder-${name} || true;
echo systemctl restart netspeeder-${name}; systemctl restart netspeeder-${name};
echo finish


